package com.aos.iot.protocol.hll;

import com.aos.iot.socket.common.log.service.MessageLogService;
import com.aos.iot.socket.common.parser.DataParser;
import com.aos.iot.socket.common.parser.DataParserFactor;
import com.aos.iot.socket.common.parser.Parser;
import com.aos.iot.socket.common.parser.ParserFactor;
import com.aos.iot.socket.common.protocol.IProtocol;
import com.aos.iot.protocol.hll.decode.HllDecoder;
import com.aos.iot.protocol.hll.encode.HllEncoder;
import com.aos.iot.protocol.hll.message.HllMessage;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import org.springframework.context.annotation.ComponentScan;

import static com.aos.iot.protocol.hll.constant.HllConstant.PROTOCOL_NAME;

@ComponentScan
public class HllProtocol implements IProtocol<HllMessage> {

    private final ParserFactor parserFactor;

    private final DataParserFactor dataParserFactor;

    private final MessageLogService messageLogService;

    public HllProtocol(ParserFactor parserFactor,
                       DataParserFactor dataParserFactor,
                       MessageLogService messageLogService) {
        this.parserFactor = parserFactor;
        this.dataParserFactor = dataParserFactor;
        this.messageLogService = messageLogService;
    }

    @Override
    public ChannelInboundHandlerAdapter decoder() {
        return new HllDecoder(this, messageLogService);
    }

    @Override
    public ChannelOutboundHandlerAdapter encoder() {
        return new HllEncoder(this, messageLogService);
    }

    @Override
    public Parser<HllMessage> getParser() {
        return parserFactor.getParser(PROTOCOL_NAME);
    }

    @Override
    public DataParser getDataParser(String cmd) {
        return dataParserFactor.getDataParser(PROTOCOL_NAME, cmd);
    }

    @Override
    public String name() {
        return PROTOCOL_NAME;
    }
}
